//
//  UserManageViewController.m
//  宜居HD
//
//  Created by 张金国 on 16/4/28.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "UserManageViewController.h"
#import "Macro.h"
#import "Masonry.h"
#import "ZHTableViewCell.h"
#import "UserMangerDetailVIewController.h"
#import "UserManageBaseViewModel.h"

@interface UserManageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)UITableView                    *tableView;

@property (nonatomic, strong)NSMutableArray                 *dataArray;//数据

@property (nonatomic, strong)NSMutableArray<NSNumber *>     *isExpland;//是否展开

@property (nonatomic ,strong)UserManageBaseViewModel        *userManageBaseViewModel;

@end

@implementation UserManageViewController
#pragma mark - LifeCircle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kDockBackgroundColor;
    self.view.frame = kViewControllerScale;
    [self loadNetModel];
    [self layoutUI];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.dataArray[section];
    if ([self.isExpland[section] boolValue]) {
        return array.count;
    }
    else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"%ld",indexPath.row];
    ZHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
        cell=[[ZHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.backgroundColor = kTableViewBackgroundColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.clipsToBounds = YES;
        cell.opaque = YES;
        
        cell.block = ^(MoreViewButtnType buttnType) {
            DDLogInfo(@"%ld",buttnType);
        };
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 80;
}
//section头部视图View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *headerSection = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    headerSection.backgroundColor = [UIColor whiteColor];
    NSString *imgName = [self.isExpland[section] boolValue]?@"arrows_bottom":@"arrows_right";
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.frame = CGRectMake(10, 40, 20, 20);
    [headerSection addSubview:imgView];
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(30, 40, 200, 20);
    label.text = [NSString stringWithFormat:@"这是分组(0/%@)",@(section)];
    [headerSection addSubview:label];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,80 - 0.5, SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = [UIColor grayColor];
    [headerSection addSubview:lineView];
    headerSection.tag = 666 + section;
    [headerSection addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return headerSection;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UserMangerDetailVIewController *userMangerDetailVIewController = [[UserMangerDetailVIewController alloc]init];
        [self presentViewController:userMangerDetailVIewController animated:YES completion:nil];});
}
#pragma mark - private
- (void)layoutUI {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.view).offset(TenOffsetViewMargin);
        make.right.bottom.equalTo(self.view);
    }];
}
- (void)loadData {
    if (!self.isExpland) {
        self.isExpland = [NSMutableArray array];
    }
    //用0代表收起，非0代表展开，默认都是收起的
    for (int i = 0; i < self.dataArray.count; i++) {
        [self.isExpland addObject:@0];
    }
    [self.tableView reloadData];
}
#pragma mark - event Response
- (void)buttonAction:(UIButton *)button {
    NSInteger section = button.tag - 666;
    //记录展开的状态
    self.isExpland[section] = [self.isExpland[section] isEqual:@0] ? @1 : @0;
    //刷新点击的section
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:section];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark - Getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView  alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = 20.0f;
        _tableView.estimatedRowHeight = 50;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.tableFooterView = [[UIView alloc]init];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
#pragma mark - load NetWork
- (void)loadNetModel {
    self.userManageBaseViewModel = [UserManageBaseViewModel new];
    self.userManageBaseViewModel.viewObject = self;
    [self.userManageBaseViewModel loadRequest];
}

- (void)setUserManage:(UserManage *)userManage {
    _userManage = userManage;
    self.dataArray = [NSMutableArray array];
    [self.dataArray addObject:userManage.commonCustomer];
    [self.dataArray addObject:userManage.importantCustomer];
    __weak __typeof__(self) weakSelf = self;
    self.userManageBaseViewModel.finishBlock = ^{
        [weakSelf loadData];
    };
}


@end
